package com.source.lightfowl.basedata.controller;

import com.source.lightfowl.basedata.entity.MachineAlert;
import com.source.lightfowl.basedata.service.MachineAlertService;
import com.source.lightfowl.utils.MyResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;
import java.util.Optional;

/**
 * @Author: Faink
 * @CreateTime: 2020/8/4 20:18
 */
@Api(tags = "报警信息记录")
@RestController
@CrossOrigin
@RequestMapping("/MachineAlert")
public class MachineAlertController {

    @Autowired
    private MachineAlertService machineAlertService;


    /**
     * 查询全部数据
     * @return
     */
    @ApiOperation(value = "查询全部数据")
    @GetMapping(value = "/all")
    public MyResult findAll(){
        return MyResult.build(0, "查询成功",machineAlertService.findAll());
    }

    /**
     * 分页查询全部数据
     * @return
     */
    @ApiOperation(value = "分页查询全部数据")
    @GetMapping(value = "/allAlert/{page}/{size}")
    public MyResult findAllAlert(@PathVariable int page, @PathVariable int size){
        return MyResult.build(0, "查询成功",machineAlertService.findAll(page, size));
    }

    /**
     * 根据ID查询，回显
     * @param id ID
     * @return
     */
    @ApiOperation(value = "根据ID查询，回显")
    @GetMapping(value = "/{id}")
    public MyResult findById(@PathVariable String id){
        MachineAlert result = machineAlertService.findById(id);
        if(result!=null)
            return MyResult.build(0,"成功",result);
        else
            return MyResult.build(1,"结果为空");
    }


    /**
     * 分页+多条件查询
     * @param searchMap 查询条件封装
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @ApiOperation(value = "分页+多条件查询")
    @GetMapping(value = "/search/{page}/{size}")
    public MyResult findSearch(@RequestBody Map searchMap , @PathVariable int page, @PathVariable int size){
        Page<MachineAlert> all = machineAlertService.findAllBySearch(searchMap, page, size);
        if(all!=null)
            return MyResult.build(0,"成功",all);
        else
            return MyResult.build(1,"结果为空");
    }


    /**
     * 修改
     * @param
     */
    @ApiOperation(value = "修改")
    @PostMapping(value="/update/alert")
    public MyResult updateMachineAlert(MachineAlert machineAlert/*String id, String barcodeMachineId, String faultInfo,
                                       String state, String remarks, Date faultTime, Date createDate,int isRead*/){
        //MachineAlert machineAlert = new MachineAlert(id, barcodeMachineId, faultInfo, state, remarks, faultTime, createDate, isRead);
        machineAlertService.update(machineAlert);
        return MyResult.ok();
    }

    /**
     * 标记已读
     */
    @ApiOperation(value = "标记已读")
    @PostMapping(value="/update/read")
    public MyResult updateRead( String id){
        machineAlertService.update(id);
        return MyResult.ok();
    }

    /**
     * 批量标记已读
     * @param ids
     */
    @ApiOperation(value = "批量标记已读")
    @PostMapping(value="/update/ids")
    public MyResult updateBatchRead(String ids){
        machineAlertService.updateBatchRead(ids);
        return MyResult.ok();
    }

    /**
     * 删除
     * @param id
     */
    @ApiOperation(value = "删除")
    @DeleteMapping(value="/{id}")
    public MyResult delete(@PathVariable String id){
        machineAlertService.deleteById(id);
        return MyResult.ok();
    }


    @ApiOperation(value = "批量删除")
    @DeleteMapping(value="/del/ids")
    public MyResult deleteBatch(String ids){
        machineAlertService.deleteBatch(ids);
        return MyResult.ok();
    }
}
